package com.marvels.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class DateUtils {

	private static final ThreadLocal<DateFormat> formatLocal1 = new ThreadLocal<DateFormat>(){
		protected DateFormat initialValue() {  
			   return new SimpleDateFormat("yyyy-MM-dd");  
			  }  
	}; 
	private static final ThreadLocal<DateFormat> formatLocal2 = new ThreadLocal<DateFormat>(){
		protected DateFormat initialValue() {  
			return new SimpleDateFormat("yyyyMMdd");  
		}  
	}; 
	private static final ThreadLocal<DateFormat> formatLocal3 = new ThreadLocal<DateFormat>(){
		protected DateFormat initialValue() {  
			return new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");  
		}  
	};
	private static final ThreadLocal<DateFormat> formatLocalhhmm = new ThreadLocal<DateFormat>(){
		protected DateFormat initialValue() {  
			return new SimpleDateFormat("HH:mm");  
		}  
	};
	private static final ThreadLocal<DateFormat> formatLocalhhmmss = new ThreadLocal<DateFormat>(){
		protected DateFormat initialValue() {  
			return new SimpleDateFormat("HH:mm:ss");  
		}  
	};
	
	private static final ThreadLocal<DateFormat> formatLocal4 = new ThreadLocal<DateFormat>(){
		protected DateFormat initialValue() {  
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		}  
	};

	
	private static final ThreadLocal<DateFormat> formatLocal5 = new ThreadLocal<DateFormat>(){
		protected DateFormat initialValue() {  
			return new SimpleDateFormat("yyyyMMddHHmmss");  
		}  
	};

	
	/**
	 * 
	 * 描述：获取当前时间前几个月时间 作者：邱霞 时间：2011-10-21 上午10:52:37
	 * 
	 * @param date
	 *            当前时间 yyyy-MM-dd字符串
	 * @param count
	 *            月数
	 * @return 返回yyyy-MM-dd字符串
	 */
	public static String getBeforeDate(String date, int count) {

		String[] item = (date.substring(0, 10)).split("-");
		int year = Integer.parseInt(item[0]);
		int month = Integer.parseInt(item[1]);
		int day = Integer.parseInt(item[2]);
		if ((month - count) <= 0) {
			month = month + 12 - count;
			year = year - 1;
		} else {
			month = month - count;
		}
		if (month < 10) {
			return year + "-" + "0" + month + "-" + day;
		} else {
			return year + "-" + month + "-" + day;
		}

	}

	/**
	 * 
	 * 描述：获取当前时间未来几个月时间 作者：邱霞 时间：2011-10-21 上午10:52:37
	 * 
	 * @param date
	 *            当前时间 yyyy-MM-dd字符串
	 * @param count
	 *            月数
	 * @return 返回yyyy-MM-dd字符串
	 */
	public static String getAfterDate(String date, int count) {
		String[] item = (date.substring(0, 10)).split("-");
		int year = Integer.parseInt(item[0]);
		int month = Integer.parseInt(item[1]);
		int day = Integer.parseInt(item[2]);
		if ((month + count) > 12) {
			month = month - 12 + count;
			year = year + 1;
		} else {
			month = month + count;
		}
		if (month < 10) {
			if (day < 10) {
				return year + "-" + "0" + month + "-" + "0" + day;
			} else {
				return year + "-" + "0" + month + "-" + day;
			}
		} else {
			if (day < 10) {
				return year + "-" + month + "-" + "0" + day;
			} else {
				return year + "-" + month + "-" + day;
			}
		}
	}

	/**
	 * 
	 * @描述：格式化日期，返回yyyy-MM-dd
	 * @作者：李炜
	 * @时间：2011-11-3 下午07:03:37
	 * @param date
	 * @return
	 */
	public static String formatDate(String date) {
		if (date == null || date.length() != 8) {
			return date;
		}
		StringBuffer buffer = new StringBuffer();
		buffer.append(date.subSequence(0, 4));
		buffer.append("-");
		buffer.append(date.subSequence(4, 6));
		buffer.append("-");
		buffer.append(date.subSequence(6, 8));
		return buffer.toString();
	}
	
	

	

//	/**
//	 * 描述：解析日期字串
//	 * 
//	 * @param dateStr
//	 *            日期字串
//	 * @return 按 yyyy-MM-dd HH:mm:ss 格式解析
//	 */
//	public static Date parseString(String dateStr) {
//		return parseString(dateStr, "HH:mm:ss");
//	}

//	/**
//	 * 描述：解析日期字串
//	 * 
//	 * @param dateStr
//	 *            日期字串
//	 * @param pattern
//	 *            字串日期格式
//	 * @return 对应日期类型数据
//	 */
//	public static Date parseString(String dateStr, String pattern) {
//		SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
//		try {
//			if (!StringUtils.isEmpty(dateStr)) {
//				return dateFormat.parse(dateStr);
//			}
//		} catch (ParseException ex) {
//			MarvelsLogUtil.getInstance().error("解析日期字符串异常",ex);
//		}
//		return null;
//	}

	
	/**
	 * 格式yyyy-MM-dd HH-mm-ss  格式化时间字符串
	 * @param dateStr 时间字符串
	 * @return
	 */
	public static Date parseString(String dateStr) {
		try {
			if (!StringUtils.isEmpty(dateStr)) {
				return formatLocal3.get().parse(dateStr);
			}
		} catch (ParseException ex) {
			MarvelsLogUtil.getInstance().error("解析日期字符串异常",ex);
		}
		return null;
	}
	
	
	public static int diffTowDate(String dateStr1,String dateStr2){
		Date date2=null;
		Date date1=null;
		try {
			date2 = formatLocal1.get().parse(dateStr2);
			date1=formatLocal1.get().parse(dateStr2);
		} catch (ParseException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>日期转换异常:",e);
		}
		int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
		return days;
		
	}
	
	
	public static int diffTowDate(Date date1,Date date2){
		int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
		return days;
		
	}
	
	/**
	 * 
	 * @Title: getDateStr @Description: 获取时间字符串 @param: @return: String @throws
	 */
	public static String getDateStr(Date date, String pattern) {
		if(date==null){
			return "";
		}
		try {
			SimpleDateFormat sb = new SimpleDateFormat(pattern);
			return sb.format(date);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "";
	}
	
	
	/**
	 * 
	 * 
	 */
	public static Date getDate(Date date, String pattern) {
		GregorianCalendar gc = new GregorianCalendar(); 
	     gc.setTimeInMillis(date.getTime());
//	     java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(pattern);
	     return gc.getTime();
	}
	
	public static Date add(Date date, int field, int amount) {
		Calendar calendar = getCalendar(date);
		calendar.add(field, amount);
		return calendar.getTime();
	}
	
	/**
	 * 以long型时间生成日历,以Calendar对象返回
	 * 
	 * @param timeStamp
	 * @return Calendar
	 */
	 public static Calendar getCalendar(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar;
	}
	 public static String getDateYYYY_MM_DD(Date date){
     	return formatLocal1.get().format(date); 
	 }
	 public static String getDateYYYY_MM_DD_HH_MM_SS(Date date){
	   return formatLocal3.get().format(date); 
	 }
	 public static Date getDateYYYY_MM_DD(String date){
		Date date1=null;
		try {
			 date1=formatLocal1.get().parse(date);
		} catch (ParseException e) {
			MarvelsLogUtil.getInstance().error(">>>>时间转换异常:",e);
		} 
     	return date1;
	}
	 public static String getDateYYYYMMDD(Date date){
     	 return formatLocal2.get().format(date); 
	 }
	 public static String getDateHHMM(Date date){
		 return formatLocalhhmm.get().format(date); 
	 }
	 public static String getDateHH_MM_SS(Date date){
		 return formatLocalhhmmss.get().format(date); 
	 }
	 
	/**
	 * 比较date1 calendarType的时间和date2的大小
	 * @param date1
	 * @param date2
	 * @param years
	 * @return
	 */
	public static boolean comparisonDate(Date date1, Date date2, int years, int calendarType) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date1);
		calendar.add(calendarType, years);
		return calendar.getTime().compareTo(date2) < 1;
	}
	
	public static  String getDateYYYYMMDDHHMMSS(Date date){
		 //	 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    	 return formatLocal4.get().format(date); 
	 }
	 
	 
	 public static  String formatDateyyyyMMddhhmmss(String dateStr) throws Exception{
		 Date date = formatLocal5.get().parse(dateStr);
		 return formatLocal4.get().format(date);
	 }
	
	/**
	 * 比较dateStr2 是否大于dateStr1  month个月
	 * @param dateStr1
	 * @param dateStr2
	 * @param month
	 * @return
	 */
	public static boolean comparisonDateStr(String dateStr1,String dateStr2,int month){
		Date date1=null;
		Date date2=null;
		try {
			date1=formatLocal4.get().parse(dateStr1);
			date2=formatLocal4.get().parse(dateStr2);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>解析时间格式异常");
		}
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date1);
		calendar.add(Calendar.MONTH, month);
		return date2.compareTo(calendar.getTime())>0;
	}
	
	
	/**
	 * 获取当前具体日期 年+"/"+月+"/"+日
	 * @return
	 */
	public static String getCurrentSpecificate(){
		Calendar calendar = Calendar.getInstance();
		int year=calendar.get(Calendar.YEAR);
		int month=calendar.get(Calendar.MONTH)+1;
		int day=calendar.get(Calendar.DATE);
		return year+"/"+month+"/"+day;
	}
	
	
	
	public static  String getYYYYMMDDHHMMSS(Date date){
		 //	 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
   	 return formatLocal5.get().format(date); 
	 }
}
